/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */
var appValidate =
{
    _errBg	   :'#FF5555',
	_dflBg	   :'#CECECE',
	_errorMark:'background',
    _regExp    :{},
    _tmp_dt    :"Test",
    _fromName  :".app_validation",
    _error     :false,
    _errLog    :Array(),
    _tipz	   :null,
    check      :function(e)
                {
					$$('.inline-error').dispose();
					appValidate._error = false;
                    $$(this).getElements('input[type=password],input[type=text],textarea,select')[0].each(function(_obj,index){

                        if(_obj.offsetHeight > 0 || _obj.hasClass('richtexteditor'))
                        {
                            appValidate.reset();
                            if(_obj.hasClass("check_notempty")){appValidate.notEmpty(_obj);}
							if(_obj.hasClass("check_notdefault")){appValidate.notDefault(_obj);}
                            if(_obj.hasClass("check_email")){appValidate.email(_obj);}
                            if(_obj.hasClass("check_password")){appValidate.password(_obj);}
                            if(_obj.hasClass("check_isimage")){appValidate.isImage(_obj);}
                            if(_obj.hasClass("check_isdoc")){appValidate.isDoc(_obj);}
                            if(_obj.hasClass("check_ispdf")){appValidate.isPdf(_obj);}
                            if(_obj.hasClass("check_isnumber")){appValidate.isNumber(_obj);}
                            if(_obj.hasClass("check_alphanumeric")){appValidate.alphaNumeric(_obj);}

                            if(appValidate._errLog.length>0)appValidate.displayErr(_obj,true);
                            else appValidate.displayErr(_obj,false);
                        }
                    });

					if(appValidate._error || _autoSubmit  == 'false')new Event(e).stop();

					if(typeof(apprain_a_s) != "undefined" && appValidate._error == false)
					{
						apprain_a_s.init();
						apprain_a_s.ajax_submit();

					}
                },
    reset      :function()
                {
                    appValidate._errLog = Array();
                },
    displayErr :function(_obj,errFlag)
                {
                    if(errFlag)
					{
						appValidate._error = true;
						if(appValidate._errToolTips != 'false')appValidate.addTTips(_obj);

						if(appValidate._errorMark=='background')
						{
							_obj.setStyle('background-color',appValidate._errBg);
						}
						else if (appValidate._errorMark=='inline')
						{
							_obj.getParent().grab(new Element('div', {'class': 'inline-error'}).set('html',appValidate._errLog),'bottom');
						}
						else
						{
							_obj.setStyle('border','1px solid ' + appValidate._errBg);
						}
					}
                    else
					{
						if(appValidate._errToolTips != 'false' && appValidate._tipz != null)appValidate._tipz.detach(_obj);
						if(appValidate._errorMark=='background')_obj.setStyle('background-color', appValidate._dflBg);
						else _obj.setStyle('border','1px solid ' + appValidate._dflBg);
					}
                },
    addTTips   :function(_obj)
                {
                    _obj.store('tip:text', appValidate._errLog.join("<br />"));
                    appValidate._tipz = new Tips( _obj,{className: 'tipz'});
                    appValidate._tipz.addEvents({
                        'show': function(tip) {tip.fade('in');},
                        'hide': function(tip) {tip.fade('out');}
                    });
                },
	addToErrLog :function(_obj,msg)
				{
					appValidate._errLog.push((_obj.get('longdesc') ? _obj.get('longdesc') : msg));
				},
    notEmpty   :function(_obj)
                {
                    if(_obj.value=="")
					{
						appValidate.addToErrLog(_obj,'This field can not be left empty');
					}
                },
	notDefault  :function(_obj)
                {
                    if(_obj.value==_obj.defaultValue)
					{
						appValidate.addToErrLog(_obj,'This field can not be left empty');
					}
                },
    email      :function(_obj)
                {
                    if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(_obj.value)))
                    {
						appValidate.addToErrLog(_obj,'Please enter a valid Email Address');
                    }
                },
	password   :function(_obj)
                {
					var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])/;

                    if(!re.test(_obj.value))
                    {
						appValidate.addToErrLog(_obj,'Enter a valid Password (Length 8 and conbination A-Za-z0-9)');
                    }
                },
	isImage	   :function (_obj)
				{
					var ext = (/[.]/.exec(_obj.value)) ? /[^.]+$/.exec(_obj.value) : undefined;
					if(ext=="gif" || ext=="jpg" || ext=="png") return true;
					else
					{
						appValidate.addToErrLog(_obj,'Invalid image file entered. Supported extensions are *.gif, *.jpg, *.jpeg, *.png');
					}
				},
	isDoc	   :function (_obj)
				{
					var ext = (/[.]/.exec(_obj.value)) ? /[^.]+$/.exec(_obj.value) : "";
					if(ext=="doc" || ext=="docx" || ext=="txt" || ext=="rtf") return true;
					else
					{
						appValidate.addToErrLog(_obj,'Invalid document file entered. Supported extensions are *.doc, *.docx, *.txt, *.rtf');
					}
				},
	isPdf      :function (_obj)
				{
					var ext = (/[.]/.exec(_obj.value)) ? /[^.]+$/.exec(_obj.value) : undefined;
					if(ext=='pdf') return true;
					else
					{
						appValidate.addToErrLog(_obj,'Invalid Pdf file entered.');
					}
				},
	alphaNumeric:function (_obj)
				{
					var re = /^[\w ]+$/;
					if(!re.test(_obj.value))
                    {
						appValidate.addToErrLog(_obj,'Aplha numeric value required');
                    }
				},
	isNumber   :function (_obj)
				{
					if(isNaN(_obj.value) || _obj.value == '')
					{
						appValidate.addToErrLog(_obj,'Please enter a valid numeric vlaue.');
					}
				},
	init	   :function(e)
                {
				    try
				    {
						appValidate._errBg = _errBg;
						appValidate._dflBg = _dflBg;
						appValidate._errToolTips = _errToolTips;
						appValidate._errorMark = _errorMark;

				    }
				    catch (err){}
					$$(appValidate._fromName).addEvent('submit', appValidate.check);
                }
}

window.addEvent('domready', appValidate.init);